/*
 * Copyright (C) 2019 Peng fei Pan <panpfpanpf@outlook.me>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package me.panpf.sketch.uri;

import me.panpf.sketch.Sketch;
import me.panpf.sketch.cache.BitmapPoolUtils;
import me.panpf.sketch.util.SketchUtils;
import me.panpf.sketch.zoom.PixmapBuild;
import ohos.app.Context;
import ohos.media.image.ImagePacker;
import ohos.media.image.ImageSource;
import ohos.media.image.PixelMap;

import java.io.OutputStream;

public abstract class AbsBitmapDiskCacheUriModel extends AbsDiskCacheUriModel<PixmapBuild> {

    @Override
    protected final void outContent(PixmapBuild bitmap, OutputStream outputStream) throws Exception {
        ImagePacker imagePacker = ImagePacker.create();
        ImagePacker.PackingOptions packingOptions = new ImagePacker.PackingOptions();
        packingOptions.quality = 100;
        imagePacker.initializePacking(outputStream, packingOptions);
        imagePacker.addImage(bitmap.getPixelMap());
        imagePacker.finalizePacking();
//        bitmap.compress(SketchUtils.bitmapConfigToCompressFormat(bitmap.getImageInfo().pixelFormat), 100, outputStream);
    }

    @Override
    protected final void closeContent(PixmapBuild bitmap, Context context) {
        BitmapPoolUtils.freeBitmapToPool(bitmap, Sketch.with(context).getConfiguration().getBitmapPool());
    }
}
